package com.ruoyi.kong.service.impl;

import java.util.List;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.kong.domain.KongLogInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kong.mapper.KongHttpLogMapper;
import com.ruoyi.kong.domain.KongHttpLog;
import com.ruoyi.kong.service.IKongHttpLogService;

/**
 * kong网关转发日志记录Service业务层处理
 * 
 * @author admin
 * @date 2021-09-18
 */
@Service
public class KongHttpLogServiceImpl implements IKongHttpLogService 
{
    @Autowired
    private KongHttpLogMapper kongHttpLogMapper;

    /**
     * 查询kong网关转发日志记录
     * 
     * @param id kong网关转发日志记录主键
     * @return kong网关转发日志记录
     */
    @Override
    public KongHttpLog selectKongHttpLogById(Long id)
    {
        return kongHttpLogMapper.selectKongHttpLogById(id);
    }

    /**
     * 查询kong网关转发日志记录列表
     * 
     * @param kongHttpLog kong网关转发日志记录
     * @return kong网关转发日志记录
     */
    @Override
    public List<KongHttpLog> selectKongHttpLogList(KongHttpLog kongHttpLog)
    {
        return kongHttpLogMapper.selectKongHttpLogList(kongHttpLog);
    }

    /**
     * 新增kong网关转发日志记录
     * 
     * @param kongHttpLog kong网关转发日志记录
     * @return 结果
     */
    @Override
    public int insertKongHttpLog(KongHttpLog kongHttpLog)
    {
        kongHttpLog.setCreateTime(DateUtils.getNowDate());
        return kongHttpLogMapper.insertKongHttpLog(kongHttpLog);
    }

    /**
     * 修改kong网关转发日志记录
     * 
     * @param kongHttpLog kong网关转发日志记录
     * @return 结果
     */
    @Override
    public int updateKongHttpLog(KongHttpLog kongHttpLog)
    {
        return kongHttpLogMapper.updateKongHttpLog(kongHttpLog);
    }

    /**
     * 批量删除kong网关转发日志记录
     * 
     * @param ids 需要删除的kong网关转发日志记录主键
     * @return 结果
     */
    @Override
    public int deleteKongHttpLogByIds(Long[] ids)
    {
        return kongHttpLogMapper.deleteKongHttpLogByIds(ids);
    }

    /**
     * 删除kong网关转发日志记录信息
     * 
     * @param id kong网关转发日志记录主键
     * @return 结果
     */
    @Override
    public int deleteKongHttpLogById(Long id)
    {
        return kongHttpLogMapper.deleteKongHttpLogById(id);
    }


    public KongHttpLog getBeanFromJsonStr(KongLogInfo kongLogInfo){
        KongHttpLog kongHttpLog = new KongHttpLog();


        // request 部分
        if (StringUtils.isNotEmpty(kongLogInfo.getRequestInfo())) {
            JSONObject jsonRequest = JSONUtil.parseObj(kongLogInfo.getRequestInfo());
            if (jsonRequest.containsKey("querystring")) {
                kongHttpLog.setRequestQuerystring(jsonRequest.getStr("querystring"));
            }

            if (jsonRequest.containsKey("headers")) {
                String requestHeader = jsonRequest.getStr("headers");
                JSONObject jsonRequestHeader = JSONUtil.parseObj(requestHeader);
                if (jsonRequestHeader.containsKey("host")) {
                    kongHttpLog.setRequestHost(jsonRequestHeader.getStr("host"));

                }
            }
            if (jsonRequest.containsKey("url")) {
                kongHttpLog.setRequestUrl(jsonRequest.getStr("url"));
            }
            if (jsonRequest.containsKey("method")) {
                kongHttpLog.setRequestMethod(jsonRequest.getStr("method"));
            }

            if (jsonRequest.containsKey("size")) {
                kongHttpLog.setRequestSize(Long.valueOf(jsonRequest.getStr("size")));
            }
            if (jsonRequest.containsKey("uri")) {
                kongHttpLog.setRequestUri(jsonRequest.getStr("uri"));
            }
            if (jsonRequest.containsKey("body")) {
                kongHttpLog.setRequestBody(jsonRequest.getStr("body"));
            }
        }


        if (StringUtils.isNotEmpty(kongLogInfo.getLatencies())) {

            JSONObject jsonLatencies = JSONUtil.parseObj(kongLogInfo.getLatencies());

            if (jsonLatencies.containsKey("kong")) {
                kongHttpLog.setLatenciesKong(Long.valueOf(jsonLatencies.getStr("kong")));
            }

            if (jsonLatencies.containsKey("request")) {
                kongHttpLog.setLatenciesRequest(Long.valueOf(jsonLatencies.getStr("request")));
            }

        }

        if (StringUtils.isNotEmpty(kongLogInfo.getResponseInfo())) {
            JSONObject jsonResponse = JSONUtil.parseObj(kongLogInfo.getResponseInfo());

            if (jsonResponse.containsKey("body")) {
                kongHttpLog.setResponseBody(jsonResponse.getStr("body"));
            }
            if (jsonResponse.containsKey("status")) {
                kongHttpLog.setResponseStatus(Integer.valueOf(jsonResponse.getStr("status")));
            }
            if (jsonResponse.containsKey("size")) {
                kongHttpLog.setResponseSize(Long.valueOf(jsonResponse.getStr("size")));
            }
        }

        if (StringUtils.isNotEmpty(kongLogInfo.getService())) {
            JSONObject jsonService = JSONUtil.parseObj(kongLogInfo.getService());
            if (jsonService.containsKey("id")) {
                kongHttpLog.setServiceId(jsonService.getStr("id"));
            }
            if (jsonService.containsKey("name")) {
                kongHttpLog.setServiceName(jsonService.getStr("name"));
            }
            if (jsonService.containsKey("host")) {
                kongHttpLog.setServiceHost(jsonService.getStr("host"));
            }
            if (jsonService.containsKey("path")) {
                kongHttpLog.setServicePath(jsonService.getStr("path"));
            }
            if (jsonService.containsKey("port")) {
                kongHttpLog.setServicePort(jsonService.getStr("port"));
            }
            if (jsonService.containsKey("ws_id")) {
                kongHttpLog.setServiceWsId(jsonService.getStr("ws_id"));
            }
            if (jsonService.containsKey("tags")) {
                kongHttpLog.setServiceTags(jsonService.getStr("tags"));
            }
        }

        if (StringUtils.isNotEmpty(kongLogInfo.getRoute())) {
            JSONObject jsonRoute = JSONUtil.parseObj(kongLogInfo.getRoute());

            if (jsonRoute.containsKey("id")) {
                kongHttpLog.setRouteId(jsonRoute.getStr("id"));
            }
            if (jsonRoute.containsKey("name")) {
                kongHttpLog.setRouteName(jsonRoute.getStr("name"));
            }
            if (jsonRoute.containsKey("hosts")) {
                kongHttpLog.setRouteHost(jsonRoute.getStr("hosts"));
            }
        }

        if (StringUtils.isNotEmpty(kongLogInfo.getConsumer())) {
            JSONObject jsonConsumer = JSONUtil.parseObj(kongLogInfo.getConsumer());
            if (jsonConsumer.containsKey("username")) {
                kongHttpLog.setConsumerName(jsonConsumer.getStr("username"));
            }
            if (jsonConsumer.containsKey("id")) {
                kongHttpLog.setConsumerId(jsonConsumer.getStr("id"));
            }
        }



        return kongHttpLog;
    }



}
